﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;

namespace IdeaSparx.CoolControls.Web
{
    public class CoolGridViewChildHost : Control, INamingContainer
    {
        private bool _IsForcedRender = false;
        private string _OriginalID = String.Empty;
        internal bool IsForcedRender
        {
            get { return _IsForcedRender; }
            set { _IsForcedRender = value; }
        }
        /// <summary>
        /// Override Render() so that this Control renders only through CustomRenderControl().
        /// The ID is stored temporarily to _OriginalID and set ID to empty string so that the control will
        /// not emmit in HTML the attribute ID="SomeID" but allows the child control to be still named
        /// with the ID prefixed to each child controls' ID by setting the bak ID to _OriginalID in RenderChildren() method.
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(HtmlTextWriter writer)
        {
            if (IsForcedRender)
            {
                _OriginalID = ID;
                ID = String.Empty;
                base.Render(writer);
            }
        }

        /// <summary>
        /// See summary of Render() method of this class.
        /// </summary>
        /// <param name="writer"></param>
        protected override void RenderChildren(HtmlTextWriter writer)
        {
            ID = _OriginalID;
            base.RenderChildren(writer);
        }

        internal virtual void CustomRenderControl(HtmlTextWriter writer)
        {
            IsForcedRender = true;
            RenderControl(writer);
            IsForcedRender = false;
        }
    }
}
